/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commonInfrastructure.accountManagement.managedbean;

import commonInfrastructure.accountManagement.entity.EmployeeEntity;
import commonInfrastructure.accountManagement.entity.RoleEntity;
import commonInfrastructure.accountManagement.session.EmployeeSessionBean;
import commonInfrastructure.accountManagement.session.RoleSessionBean;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Wei Haoyang
 */
@ManagedBean
@ViewScoped
public class ViewInfoManagedBean {

    @EJB
    private EmployeeSessionBean em;
    @EJB
    private RoleSessionBean rm;
    private EmployeeEntity selectedEmployee;
    private boolean editMode;
    private Long id;
    private List<String> selectedRoles;
    /** Creates a new instance of ViewInfoManagedBean */
    public ViewInfoManagedBean() {
        selectedEmployee = new EmployeeEntity();
    }
    
    public boolean isEditMode() {  
        return editMode;  
    }
    
    public EmployeeEntity getEmployees() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Long loginId = (Long) request.getSession().getAttribute("userId");
        return em.getEmployeeById(loginId);
    }

    public void deleteEmployee(ActionEvent event) {
        setId((Long)event.getComponent().getAttributes().get("code1"));
        getEm().removeEmployee(getId());
    }
    
    public void saveChanges(ActionEvent event)
    {
        em.editEmployee(selectedEmployee);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Changes saved.", ""));        
    }

    /**
     * @return the em
     */
    public EmployeeSessionBean getEm() {
        return em;
    }

    /**
     * @param em the em to set
     */
    public void setEm(EmployeeSessionBean em) {
        this.em = em;
    }

    /**
     * @return the selectedEmployee
     */
    public EmployeeEntity getSelectedEmployee() {
        return selectedEmployee;
    }

    /**
     * @param selectedEmployee the selectedEmployee to set
     */
    public void setSelectedEmployee(EmployeeEntity selectedEmployee) {
        this.selectedEmployee = selectedEmployee;
    }

    /**
     * @param editMode the editMode to set
     */
    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }
    
    public String toSentence(List<RoleEntity> roles){
        String output = null;
        StringBuffer sb = new StringBuffer();
        int i = 0;
        while (i < roles.size()){
            sb.append(roles.get(i).getRoleName());
            sb.append(";");
            System.out.println(roles.get(i).getRoleName());
            i++;
        }
        output = sb.toString();
        return output;
    }

    public List<String> getSelectedRoles() {
        return selectedRoles;
    }

    public void setSelectedRoles(List<String> selectedRoles) {
        this.selectedRoles = selectedRoles;
    }
    
    public void pushToRoles(EmployeeEntity selectedEmployee){
        int i = 0;
        Long id;
        id = Long.valueOf(selectedRoles.get(i));
        selectedEmployee.getRoles().add(getRm().getRole(id));
        System.out.println(selectedRoles.get(i));
        
        while(i < (selectedRoles.size()-1)){
            i++;
            id = Long.valueOf(selectedRoles.get(i));
            selectedEmployee.getRoles().add(getRm().getRole(id));
            System.out.println(selectedRoles.get(i));
        }
        
    }

    public RoleSessionBean getRm() {
        return rm;
    }

    public void setRm(RoleSessionBean rm) {
        this.rm = rm;
    }
    
}
